package apartmentRentGWT.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class aparentRent implements EntryPoint {
  /**
   * The message displayed to the user when the server cannot be reached or
   * returns an error.
   */
  private static final String  SERVER_ERROR = "An error occurred while "
      + "attempting to contact the server. Please check your network "
      + "connection and try again.";
  /**
   * Create a remote service proxy to talk to the server-side Greeting service.
   */
  private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

  private final Messages messages = GWT.create(Messages.class);


  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
    final Button sendButton = new Button( messages.sendButton() );
    final Button restartButton = new Button( messages.restartButton() );


    // We can add style names to widgets
    sendButton.addStyleName("sendButton");
    restartButton.addStyleName("sendButton");
      VerticalPanel panel = new VerticalPanel();
    // Add the nameField and sendButton to the RootPanel
    // Use RootPanel.get() to get the entire body element
    //RootPanel.get("nameFieldContainer").add(nameField);
    RootPanel.get("sendButtonContainer").add(sendButton);
    RootPanel.get("restartButtonContainer").add(restartButton);
    RootPanel.get("tableArea").add(panel);
    final FlexTable flexTable = new FlexTable();
      panel.add(flexTable);
      setupTableHeader(flexTable);

      final SimulationHandler simulationHandler = new SimulationHandler();
      simulationHandler.init();
      flexTable.addStyleName("demo-table demo-panel");

      class RestartButtonHandler implements    ClickHandler{
        public void onClick(ClickEvent event) {
            sendButton.setEnabled(true);
            RootPanel.get("imageContainer").clear();
            flexTable.removeAllRows();   // clear data and html
            setupTableHeader(flexTable);
            simulationHandler.init();
        }
    }
    // Create a handler for the sendButton and nameField
    class MyHandler implements ClickHandler, KeyUpHandler {
      /**
       * Fired when the user clicks on the sendButton.
       */
      public void onClick(ClickEvent event) {
        simulate();
      }

      /**
       * Fired when the user types in the nameField.
       */
      public void onKeyUp(KeyUpEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            simulate();
        }
      }

      private void simulate(){

          Choice choice = simulationHandler.simulate();
          if(choice==null){
                 sendButton.setEnabled(false);
          }
          else{
              int row = flexTable.getRowCount();
              flexTable.setWidget(row, 0, new Label((row-1)/3+1+""));
              flexTable.setWidget(row, 1, new Label(choice.getName()));
              flexTable.setWidget(row, 2, new Label(choice.getFirstPlayerChoice()));
              flexTable.setWidget(row, 3, new Label(choice.getSecondPlayerChoice()));
              flexTable.setWidget(row, 4, new Label(simulationHandler.getCurrentMoneyPlayer1()+""));
              flexTable.setWidget(row, 5, new Label(simulationHandler.getCurrentMoneyPlayer2()+""));
              for(int i=0;i<6;i++){
                  flexTable.getFlexCellFormatter().setStyleName(row,i,"demo-table-cell");
              }
              RootPanel.get("imageContainer").clear();
              RootPanel.get("imageContainer").add(new Image(simulationHandler.getImage()));

          }

      }
    }


    // Add a handler to send the name to the server
    MyHandler handler = new MyHandler();
    sendButton.addClickHandler(handler);
    sendButton.addKeyUpHandler(handler);
    restartButton.addClickHandler(new RestartButtonHandler());
    //nameField.addKeyUpHandler(handler);
  }

    private void setupTableHeader(FlexTable flexTable){
            flexTable.setWidget(0, 0, new Label("Round| "));
            flexTable.setWidget(0, 1, new Label("Activity| "));
            flexTable.setWidget(0, 2, new Label("Player1 Choice| "));
            flexTable.setWidget(0, 3, new Label("Player2 Choice| "));
            flexTable.setWidget(0, 4, new Label("Player1 money| "));
            flexTable.setWidget(0, 5, new Label("Player2 money| "));

    }
}
